package cn.cdutetc.library.controller;

import cn.cdutetc.library.common.ApiResponse;
import cn.cdutetc.library.entity.dto.NotificationDTO;
import cn.cdutetc.library.entity.dto.NotificationVO;
import cn.cdutetc.library.service.NotificationService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 通知管理控制器
 */
@RestController
@RequestMapping("/api/notifications")
@Tag(name = "通知管理", description = "通知管理相关接口")
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    @PostMapping
    @Operation(summary = "创建通知", description = "创建新的通知")
    public ApiResponse<Integer> createNotification(@RequestBody @Validated NotificationDTO notificationDTO) {
        Integer notificationId = notificationService.createNotification(notificationDTO);
        return ApiResponse.success(notificationId);
    }

    @PostMapping("/system")
    @Operation(summary = "创建系统通知", description = "创建系统通知")
    public ApiResponse<Boolean> createSystemNotification(@RequestBody @Validated NotificationDTO notificationDTO) {
        boolean result = notificationService.createSystemNotification(notificationDTO);
        return ApiResponse.success(result);
    }

    @PostMapping("/{notificationId}/read")
    @Operation(summary = "标记通知为已读", description = "标记通知为已读")
    public ApiResponse<Boolean> markAsRead(@PathVariable @Parameter(description = "通知ID") Integer notificationId) {
        boolean result = notificationService.markAsRead(notificationId);
        return ApiResponse.success(result);
    }

    @PostMapping("/batch/read")
    @Operation(summary = "批量标记通知为已读", description = "批量标记通知为已读")
    public ApiResponse<Boolean> batchMarkAsRead(@RequestBody List<Integer> notificationIds) {
        boolean result = notificationService.batchMarkAsRead(notificationIds);
        return ApiResponse.success(result);
    }

    @PostMapping("/user/{userId}/read-all")
    @Operation(summary = "标记用户所有通知为已读", description = "标记用户所有通知为已读")
    public ApiResponse<Boolean> markAllAsRead(@PathVariable @Parameter(description = "用户ID") Integer userId) {
        boolean result = notificationService.markAllAsRead(userId);
        return ApiResponse.success(result);
    }

    @DeleteMapping("/{notificationId}")
    @Operation(summary = "删除通知", description = "根据ID删除通知")
    public ApiResponse<Boolean> deleteNotification(@PathVariable @Parameter(description = "通知ID") Integer notificationId) {
        boolean result = notificationService.deleteNotification(notificationId);
        return ApiResponse.success(result);
    }

    @GetMapping("/{notificationId}")
    @Operation(summary = "获取通知详情", description = "根据ID获取通知详情")
    public ApiResponse<NotificationVO> getNotificationById(@PathVariable @Parameter(description = "通知ID") Integer notificationId) {
        NotificationVO notificationVO = notificationService.getNotificationById(notificationId);
        return ApiResponse.success(notificationVO);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询用户通知", description = "分页查询用户通知")
    public ApiResponse<IPage<NotificationVO>> pageUserNotifications(
            @RequestParam(defaultValue = "1") @Parameter(description = "页码") Integer page,
            @RequestParam(defaultValue = "10") @Parameter(description = "每页数量") Integer size,
            @RequestParam(required = false) @Parameter(description = "用户ID") Integer userId,
            @RequestParam(required = false) @Parameter(description = "通知类型") Integer type,
            @RequestParam(required = false) @Parameter(description = "是否已读") Boolean isRead) {
        IPage<NotificationVO> result = notificationService.pageUserNotifications(page, size, userId, type, isRead);
        return ApiResponse.success(result);
    }

    @GetMapping("/user/{userId}/unread-count")
    @Operation(summary = "获取用户未读通知数量", description = "获取用户未读通知数量")
    public ApiResponse<Integer> countUnreadNotifications(@PathVariable @Parameter(description = "用户ID") Integer userId) {
        Integer count = notificationService.countUnreadNotifications(userId);
        return ApiResponse.success(count);
    }
}